package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/13 19:42
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    //注入orderSettingDao操作预约设置表
    @Autowired
    private OrderSettingDao orderSettingDao;
    //注入memberDao操作会员表
    @Autowired
    private MemberDao memberDao;
    //注入orderDao操作预约表
    @Autowired
    private OrderDao orderDao;



     /*
    移动端-->根据id查询预约信息(套餐信息,会员信息)
     */
    @Override
    public Map findById(Integer id) throws Exception {
        //根据orderDao完成查询
        Map map = orderDao.findById4Detail(id);
        if(map != null){//执行查询成功
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }





        /*
       移动端-->体检预约提交
        */
    @Override
    public Result order(Map map) throws Exception{
        //1.查询当前日期是否进行了预约设置
        //取出用户输入的日期
        String  orderDate = (String) map.get("orderDate");
        //调用dateUtils工具类将日期转换成date格式
        Date date = DateUtils.parseString2Date(orderDate);
        //调用orderSettingDao完成查询,封装到预约设置对象中
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if(orderSetting == null){//当前日期没有进行预约设置,不能预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.查询预约日期是否预约已满(可预约人数,已预约人数)
        //获取参数,t_ordersetting
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(number <= reservations){//预约已满,不能进行预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //3.查询当前用户是否为会员
        //获取telephone
        String telephone = (String) map.get("telephone");
        //4.调用memberDao完成telephone进行查询是否为会员
        Member member = memberDao.findByTelephone(telephone);
        //防止重复预约(同一天内同一个用户同一个套餐)
        if(member != null){//用户为会员
            Integer memberId = member.getId();//会员id
            int setmealId = Integer.parseInt((String)map.get("setmealId"));//套餐id
            //创建预约实体类进行封装数据
            Order order = new Order(memberId,date,null,null,setmealId);
            //根据预约信息条件查询是否存在
            List<Order> list = orderDao.findByCondition(order);
            if(list != null && list.size() > 0 ){//已经完成了预约,
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }
        //5.预约成功,更改预约人数
        //将预约设置表中当前预约人数加一
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        //通过预约时间修改可预约人数
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        if(member == null){//当前用户不是会员
            //将此用户添加到会员表
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setFileNumber((String) map.get("fileNumber"));
            member.setRegTime(new Date());
            memberDao.add(member);

        }
        //6.保存预约信息到预约表
        Order order = new Order(member.getId(),date,(String) map.get("orderType"),Order.ORDERSTATUS_NO,Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }
}
